from functools import wraps
from fastapi import Request
from ..services.auth_service import AuthService


def token_required(func):
    """
    装饰器：验证token
    :return: 用户id
    """
    @wraps(func)
    async def wrapper(request: Request, *args, **kwargs):
        auth_service = AuthService()
        # 获取用户id，并设置到request.state.user_id
        user_id = await auth_service.verify_token(request)
        request.state.user_id = user_id
        return await func(request, *args, **kwargs)
    return wrapper
